package com.vir.ai.admin.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

import org.springframework.util.Base64Utils;
import org.springframework.util.DigestUtils;

import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.security.spec.KeySpec;
import java.util.Base64;

/**
 * 符合业界安全标准的密码hash处理,涉及的加密算法不要改动！！！
 * **/
@Slf4j
public class PasswordHashUtil {
	public static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA256";
	public static final int SALT_SIZE = 32;
	public static final int HASH_SIZE = 256;
	public static final int PBKDF2_ITERATIONS = 10000;

	public static void main(String[] args) throws Exception {
		 
	}

	public static String decriptAES(String SecretText, String key, String iv) throws Exception {
		try {

			byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
			byte[] ivBytes = iv.getBytes(StandardCharsets.UTF_8);

			SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
			IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);

			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

			cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);

			byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(SecretText));
			String decryptedResult = new String(decryptedBytes, StandardCharsets.UTF_8);
			
			return decryptedResult;
		}
		catch (Exception e) {
			String msg = "AES解密失败 - " + e.getMessage();
			log.error(msg, e);
			throw new RuntimeException(msg);
		}

	}
	
 
}
